package com.xiaomengkuan.product.controller;

import com.xiaomengkuan.common.entity.vo.PageVo;
import com.xiaomengkuan.common.util.DetailSelector;
import com.xiaomengkuan.common.util.R;
import com.xiaomengkuan.product.entity.tdo.TDO;
import com.xiaomengkuan.product.entity.vo.ProductResponseVo;
import com.xiaomengkuan.product.service.LikeAndFavouriteService;
import com.xiaomengkuan.product.util.RelationSelector;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author Faraj Mujey
 * -- Creation Time: 2021-01-27 2:08 下午, File Encoding: utf8 --
 */
@RestController
@RequestMapping("product")
@Api(tags = "收藏商品")
public class FavouriteController {
    @Resource
    private LikeAndFavouriteService likeAndFavouriteService;

    /**
     * 收藏某件商品
     *
     * @param userId    用户id
     * @param productId 商品id
     * @return R
     */
    @ApiOperation(value = "收藏某件商品")
    @GetMapping("favourite/{userId}/{productId}")
    public R favourite(@PathVariable("userId") String userId,
                       @PathVariable("productId") String productId) {
        TDO flag = likeAndFavouriteService.like(userId, productId,
                RelationSelector.FAVOURITE);
        return flag.isFlag() ? R.ok().detail(flag.getDetail()) :
                R.err().detail(flag.getDetail());
    }

    /**
     * 取消收藏某件事商品
     *
     * @param userId    用户id
     * @param productId 商品id
     * @return R
     */
    @ApiOperation(value = "取消收藏某件商品")
    @GetMapping("unfavourite/{userId}/{productId}")
    public R unfavourite(@PathVariable("userId") String userId,
                         @PathVariable("productId") String productId) {
        TDO flag = likeAndFavouriteService.unlike(userId, productId,
                RelationSelector.FAVOURITE);
        return flag.isFlag() ? R.ok().detail(flag.getDetail()) :
                R.err().detail(flag.getDetail());
    }

    @ApiOperation(value = "获取当前用户的收藏数")
    @GetMapping("favouriteCount/{userId}")
    public R favouriteCount(@PathVariable("userId") String userId) {
        long count = likeAndFavouriteService.favouriteCount(userId);
        return R.ok().detail(DetailSelector.QUERY_SUCCESS).putItem(count);
    }

    @ApiOperation(value = "分页获取收藏的商品")
    @PostMapping("getFavourites")
    public R getFavourites(@RequestBody Map<String, Object> requestBody) {
        PageVo<ProductResponseVo> pageVo =
                likeAndFavouriteService.getFavourites(requestBody);
        return R.ok().detail(DetailSelector.QUERY_SUCCESS).page(pageVo);
    }
}
